const tree = (list, id = 'id', fid = 'fid') => {
	if (!list) {
		return;
	}
	const map = {},
		tree = [];
	for (const item of list) {
		map[item[id]] = item;
	}
	for (const item of list) {
		const parent = map[item[fid]];

		if (parent) {
			(parent.children || (parent.children = [])).push(item);
		} else {
			tree.push(item);
		}
	}
	return tree;
}
const getIds = (arr) => {
	let ids = []
	for (let i = 0; i < arr.length; i++) {
		if (arr[i].children && arr[i].children.length > 0) {
			ids.push(arr[i].id)
			ids = ids.concat(getIds(arr[i].children))
		} else {
			ids.push(arr[i].id)

		}
	}

	return ids.length ? ids.join(',') : ''
}
const sortDownDate = (a, b) => {
	return b.time - a.time
}
const reduce = (arr, idName = 'id') => {
	arr = arr.sort(sortDownDate)
	let obj = {};
	let reduce = [];
	reduce = arr.reduce(function(item, next) {
		//item为没有重复id的数组，next为当前对象
		obj[next[idName]] ? '' : (obj[next[idName]] = true && item.push(next));
		return item;
	}, []);
	return reduce
}
module.exports = {
	tree,
	getIds,
	reduce
}